package cn.message.web;

import cn.itcast.util.OutputStringController;
import cn.message.service.IPerformanceService;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * @author 郭智超
 * @create 2020-04-10 17:54
 */
@Slf4j
@RestController
@RequestMapping(value="/dhn/message",produces="text/html;charset=UTF-8")
public class PerformanceController {

    @Resource
    private IPerformanceService performanceService;

    @Resource
    private OutputStringController osc;

    @RequestMapping("/getAllPerformance")
    @ResponseBody
    public String getAllPerformance(@RequestBody Map<String,String> map)  {
        String results = "";
        //获取前台提交的数据
        Integer page = Integer.parseInt(map.get("page"));
        Integer limit = Integer.parseInt(map.get("limit"));
        String horsemanName = map.get("horsemanName");
        String adminPhone = map.get("adminPhone");
        String role = map.get("role");
        String[] dateRanges = {};
        Date startDate = null;
        Date endDate = null;
        if (!map.get("dateRange").isEmpty()) {
            dateRanges = map.get("dateRange").split(",");
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                startDate = sdf.parse(dateRanges[0]);
                endDate = sdf.parse(dateRanges[1]);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        Map<String, Object> allPerformance = performanceService.getAllPerformance(page,limit,startDate,endDate,horsemanName,adminPhone,role);
        Integer count = (Integer)allPerformance.get("count");
        Object performanceArrayList = allPerformance.get("orders");

        if(performanceArrayList == null){
            results = osc.failure("查询绩效失败");
        }else {
            results = osc.resultSuccess("查询绩效成功",performanceArrayList,count);
        }

        return results;
    }

    @RequestMapping("/getAllPerformanceItem")
    @ResponseBody
    public String getAllPerformanceItem(@RequestBody Map<String,String> map){
        String results = "";
        //获取前台提交的数据
        Integer page = Integer.parseInt(map.get("page"));
        Integer limit = Integer.parseInt(map.get("limit"));
        String horsemanOpenId = map.get("horsemanOpenId");

        String[] dateRanges = {};
        Date startDate = null;
        Date endDate = null;
        if (!map.get("dateRange").isEmpty()) {
            dateRanges = map.get("dateRange").split(",");
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                startDate = sdf.parse(dateRanges[0]);
                endDate = sdf.parse(dateRanges[1]);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        Map<String, Object> allPerformanceItem = performanceService.getAllPerformanceItem(page,limit,horsemanOpenId,startDate,endDate);
        Integer count = (Integer)allPerformanceItem.get("count");
        Object performanceArrayList = allPerformanceItem.get("orderItems");

        if(performanceArrayList == null){
            results = osc.failure("查询绩效明细失败");
        }else {
            results = osc.resultSuccess("查询绩效明细成功",performanceArrayList,count);
        }

        return results;
    }
}
